<?php
/**
 * Created by PhpStorm.
 * User: MG
 * Date: 2021/5/26
 * Time: 16:11
 */

namespace app\api\controller;


use app\admin\model\ChargePile;
use app\admin\model\MaintainPile;
use app\common\controller\Api;
use app\common\service\Location;
use Endroid\QrCode\QrCode;

class InstallPile extends Api
{
    protected $noNeedLogin = ['landlordCode','lst'];
    protected $noNeedRight = ['installUpload','pillList','grabSingle','masterInstall','uploadFault',
        'maintainList','maintain','uploadMaintain','landlord','installTime','addChargePile','installInfo'];

    /**
     * 布点充电桩信息上传
     */
    public function installUpload()
    {
        $uid=$this->auth->id;
        $landlord_name=$this->request->request('landlord_name');
        $landlord_mobile=$this->request->request('landlord_mobile');
        $village=$this->request->request('village');
        $address=$this->request->request('address');
        $longitude=$this->request->request('longitude');
        $latitude=$this->request->request('latitude');
        $images=$this->request->request('images');
        $data['landlord_name']=$landlord_name;
        $data['orderid']=date("YmdHis") . mt_rand(100000, 999999);
        $data['landlord_mobile']=$landlord_mobile;
        $data['village']=$village;
        $data['address']=$address;
        $data['longitude']=$longitude;
        $data['latitude']=$latitude;
        $data['images']=$images;
        $data['user_id']=$uid;
        $data['is_install']=0;
        $data['createtime']=time();
        $res=\app\admin\model\InstallPile::create($data);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     * 布点充电桩列表
     */
    public function pillList()
    {
        $uid=$this->auth->id;
        $search=$this->request->request('search','');
        $type=$this->request->request('type',0);
        $page=$this->request->request('page',1);
        $limit=$this->request->request('limit',10);
        $longitude=$this->request->request('longitude');
        $latitude=$this->request->request('latitude');
        \app\admin\model\InstallPile::update(['endtime'=>null,'p_user_id'=>0,'is_install'=>0],['endtime'=>['lt',time()],'is_install'=>['neq',2]]);
        $list=\app\admin\model\InstallPile::where('is_install','eq',$type);
        $count=\app\admin\model\InstallPile::where('is_install','eq',$type);
        $EARTH=6378.137; //地球半径
        $PI=3.1415926535898; //PI值
        if (!empty($search)) {
            $list=$list->where('orderid','like','%'.$search.'%');
            $count=$count->where('orderid','like','%'.$search.'%');

        }
        if ($type!=0) {
            $list=$list->where('p_user_id',$uid);
            $count=$count->where('p_user_id',$uid);
        }
        $count=$count->count();
        $list=$list->field("*,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$latitude."-latitude)/360),2)+COS($PI*".$latitude."/180)* COS(latitude * $PI/180)*POW(SIN($PI*(".$longitude."-longitude)/360),2)))) as distance")
            ->limit($page*$limit-$limit,$limit)
            ->order('distance asc')
            ->select();

        if (count($list)>0){
            foreach ($list as &$v){
                $v['distance']=round($v['distance'],1);
                if (!empty($v['images'])){
                    $images=explode(',',$v['images']);
                    for ($i=0;$i<count($images);$i++){
                        $images[$i]=cdnurl($images[$i]);
                    }
                    $v['images']=$images;
                }
            }

//            $res=array_column($list,'distance');
//            array_multisort($res,SORT_ASC,$list);
        }

        $this->success('成功',compact('count','list'));
    }

    /**
     * 师傅抢单
     */
    public function grabSingle()
    {
        $uid=$this->auth->id;
        $id=$this->request->request('id',0);
        if ($id==0) $this->error('请选择抢单信息');
        $res=\app\admin\model\InstallPile::update(['is_install'=>1,'p_user_id'=>$uid,'endtime'=>time()+60*60*2],['id'=>$id]);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     * 师傅上传安装信息
     */
    public function masterInstall()
    {
        $id=$this->request->request('id',0);
        $info=$this->request->request('info','');
        $images=$this->request->request('images');
        $res=\app\admin\model\InstallPile::update(['info'=>$info,'pimages'=>$images,'is_install'=>2],['id'=>$id]);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     * 上报故障信息
     */
    public function uploadFault()
    {

        $device_number=$this->request->request('device_number');
        $village=$this->request->request('village');
        $address=$this->request->request('address');
        $longitude=$this->request->request('longitude');
        $latitude=$this->request->request('latitude');
        $images=$this->request->request('images');
        $info=$this->request->request('info');
        $data['device_number']=$device_number;
        $data['village']=$village;
        $data['address']=$address;
        $data['longitude']=$longitude;
        $data['latitude']=$latitude;
        $data['images']=$images;
        $data['maintain_info']=$info;
        $data['info']=$info;
        $data['orderid']=date("YmdHis") . mt_rand(100000, 999999);
        $data['is_maintain']=0;
        $data['createtime']=time();
        $res=MaintainPile::create($data);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     * 维修列表
     */
    public function maintainList()
    {
        $uid=$this->auth->id;
//        $uid=133;
        $search=$this->request->request('search','');
        $type=$this->request->request('type',0);
        $page=$this->request->request('page',1);
        $limit=$this->request->request('limit',10);
        $longitude=$this->request->request('longitude');
        $latitude=$this->request->request('latitude');
        $list=MaintainPile::where('is_maintain','eq',$type);
        $count=MaintainPile::where('is_maintain','eq',$type);
        $EARTH=6378.137; //地球半径
        $PI=3.1415926535898; //PI值
        MaintainPile::update(['endtime'=>null,'p_user_id'=>0,'is_maintain'=>0],['endtime'=>['lt',time()],'is_maintain'=>['neq',2]]);

        if (!empty($search)) {
            $list=$list->where('orderid','like','%'.$search.'%');
            $count=$count->where('orderid','like','%'.$search.'%');

        }
        if ($type!=0) {
            $list=$list->where('p_user_id',$uid);
            $count=$count->where('p_user_id',$uid);
        }
        $count=$count->count();
        $list=$list->field("*,(2 * $EARTH* ASIN(SQRT(POW(SIN($PI*(".$latitude."-latitude)/360),2)+COS($PI*".$latitude."/180)* COS(latitude * $PI/180)*POW(SIN($PI*(".$longitude."-longitude)/360),2)))) as distance")
        ->limit($page*$limit-$limit,$limit)
            ->order('distance asc')
            ->select();

        if (count($list)>0){
            foreach ($list as &$v){
                $v['distance']=round($v['distance'],1);
                if (!empty($v['images'])){
                    $images=explode(',',$v['images']);
                    for ($i=0;$i<count($images);$i++){
                        $images[$i]=cdnurl($images[$i]);
                    }
                    $v['images']=$images;
                }
            }

//            $res=array_column($list,'distance');
//            array_multisort($res,SORT_ASC,$list);
        }

        $this->success('成功',compact('count','list'));
    }

    /**
     * 维修师傅抢单
     */
    public function maintain()
    {
        $uid=$this->auth->id;
//        $uid=133;
        $id=$this->request->request('id',0);
        if ($id==0) $this->error('请选择抢单信息');
        $res=MaintainPile::update(['is_maintain'=>1,'p_user_id'=>$uid,'endtime'=>time()+60*60*2],['id'=>$id]);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     * 上传维修信息
     */
    public function uploadMaintain()
    {
        $uid=$this->auth->id;
        $id=$this->request->request('id',0);
        $info=$this->request->request('info','');
        $images=$this->request->request('images');
        $res=MaintainPile::update(['info'=>$info,'pimages'=>$images,'is_maintain'=>2,'p_user_id'=>$uid],['id'=>$id]);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     * 成为房东二维码
     */
    public function landlordCode()
    {
        $qrcode=new QrCode();
        $url='http://yp.youjukj.com/index.html#/pages/home/landlord';
        $path=ROOT_PATH.'/public/fangdong/fangdong.png';
        $qrcode->setText($url);
        $qrcode->writeFile($path);
        header('Content-Type: '.$qrcode->getContentType());
        echo $qrcode->writeString();
        exit;
    }

    /**
     * 修改用户成为房东
     */
    public function landlord()
    {
        $id=$this->auth->id;
        $res=\app\admin\model\User::update(['is_landlord'=>1],['id'=>$id]);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     *未安装列表
     */
    public function lst()
    {
        $uid=$this->request->request('uid',0);
//        $uid=132;
        $user=\app\admin\model\User::where('id',$uid)->find();
        $list=\app\admin\model\InstallPile::where('is_install','between',[0,1])->field('orderid as name,id,longitude,latitude')->select();
        $type=config('site.identity_show');

        if ($type==1&&$user['is_landlord']==1) {
            $res=$list;
        }else if ($type==2&&$user['is_bazaar']==1) {
            $res=$list;
        }else if ($type==3&&$user['is_master']==1) {
            $res=$list;
        }else if ($type==0) {
            $res=$list;
        } else $res=[];
        $this->success('成功',$res);
    }


    /**
     * 添加充电桩
     */
    public function addChargePile()
    {
        $device_number=$this->request->request('device_number');
        $manufacturer_id=$this->request->request('manufacturer_id');
        $operator_id=$this->request->request('operator_id');
        $operator_code=$this->request->request('operator_code');
        $kind_id=$this->request->request('kind_id');
        $street=$this->request->request('street');
        $community=$this->request->request('community');
        $village=$this->request->request('village');
        $address=$this->request->request('address');
        $city=$this->request->request('city');
        $longitude=$this->request->request('longitude');
        $latitude=$this->request->request('latitude');
        $port=$this->request->request('port');
        $installtime=$this->request->request('installtime');
        $meter_code=$this->request->request('meter_code');
        $unit_type=$this->request->request('unit_type');
        $powers=$this->request->request('powers');
        $install_way=$this->request->request('install_way');
        $recharge_way=$this->request->request('recharge_way');
        $images=$this->request->request('images');
        $files=$this->request->request('files');
        $id=$this->request->request('id');
        if (empty($device_number)||empty($manufacturer_id)||empty($operator_id)||
            empty($operator_code)||empty($kind_id)||empty($street)||empty($community)||
            empty($village)||empty($address)||empty($city)||empty($longitude)||empty($latitude)||
            empty($port) ||empty($installtime) || empty($meter_code || empty($images) || empty($files))
        ) $this->error('请填写必填项');
        $citys=explode('/',$city);
        $addData['device_number']=$device_number;
        $addData['manufacturer_id']=$manufacturer_id;
        $addData['operator_id']=$operator_id;
        $addData['operator_code']=$operator_code;
        $addData['kind_id']=$kind_id;
        $addData['street']=$street;
        $addData['community']=$community;
        $addData['village']=$village;
        $addData['address']=$address;
        $addData['city']=$city;
        $addData['province']=$citys[0];
        $addData['citys']=$citys[1];
        $addData['area']=$citys[2];
        $addData['longitude']=$longitude;
        $addData['latitude']=$latitude;
        $addData['port']=$port;
        $addData['installtime']=strtotime($installtime);
        $addData['meter_code']=$meter_code;
        $addData['user_id']=$this->auth->id;
        $addData['createtime']=time();
        $addData['images']=$images;
        $addData['files']=$files;
        $addData['unit_type']=$unit_type;
        $addData['powers']=$powers;
        $addData['install_way']=$install_way;
        $addData['recharge_way']=$recharge_way;
        $addData['status']=0;
        $res=ChargePile::create($addData);
        \app\admin\model\InstallPile::update(['pimages'=>$images,'is_install'=>2,'charge_pile_id'=>$res['id']],['id'=>$id]);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     *延长安装时间
     */
    public function installTime()
    {
        $id=$this->request->request('id');
        $res=\app\admin\model\InstallPile::where('id',$id)->setInc('endtime',60*60);
        if ($res) $this->success('成功');
        else $this->error('失败');
    }

    /**
     * 安装详情
     */
    public function installInfo()
    {
        $id=$this->request->request('id');
        $res=\app\admin\model\InstallPile::where('id',$id)->find();
        if ($res) $this->success('成功',$res);
        else $this->error('失败');
    }

}